//
//  ShoppingCartViewController.m
//  Mahjong
//
//  Created by mc on 16/8/29.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "ShoppingCartViewController.h"
#import "Header.h"
#import "ShopCarTableViewCell.h"
#import "ShopCarModel.h"
@interface ShoppingCartViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ShoppingCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT-50) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = KRGBColor(239, 239, 239);
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerNib:[UINib nibWithNibName:@"ShopCarTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self addView];
    if (self.type == 1) {
        self.navigationItem.title = @"购物车";
        UIColor * color = [UIColor blackColor];
        NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
        self.navigationController.navigationBar.titleTextAttributes = dict;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_but_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    }
    self.dataArray = [NSMutableArray array];
    [self createData];
}
-(void)leftAction:(UIBarButtonItem *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addView{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENH_HEIGHT-96, SCREEN_WIDTH, 50)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    if (self.type == 1) {
        backView.frame = CGRectMake(0, SCREENH_HEIGHT-50, SCREEN_WIDTH, 50);
    }
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"选择1"] forState:UIControlStateSelected];
    button.frame = CGRectMake(4, 13, 20, 20);
    [backView addSubview:button];
    [button addTarget:self action:@selector(seleAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 15, 44, 22)];
    label.text = @"全选";
    [backView addSubview:label];
    
    UILabel *conLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 15, 100, 22)];
    conLabel.text = @"合计 : 123";
    conLabel.textColor = [UIColor redColor];
    [backView addSubview:conLabel];
    
    UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    okButton.frame = CGRectMake(SCREEN_WIDTH-SCREEN_WIDTH/5*2, 0, SCREEN_WIDTH/5*2, 25);
    okButton.backgroundColor = [UIColor redColor];
    [okButton setTitle:@"结算" forState:UIControlStateNormal];
    [backView addSubview:okButton];
    
    UIButton *okbButton = [UIButton buttonWithType:UIButtonTypeCustom];
    okbButton.frame = CGRectMake(SCREEN_WIDTH-SCREEN_WIDTH/5*2, 25, SCREEN_WIDTH/5*2, 25);
    okbButton.backgroundColor = [UIColor redColor];
    [okbButton setTitle:@"不含运费" forState:UIControlStateNormal];
    okbButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [backView addSubview:okbButton];
    
}
//全选按钮
-(void)seleAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"carSele" object:@"1"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"carSele"];
//        [self.myTableView reloadData];
    }
    if (!btn.selected){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"carSele" object:@"0"];

//        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"carSele"];
//        [self.myTableView reloadData];
    }
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    //tableview自带编辑方法
    [self.myTableView setEditing:editing animated:animated];
    
}
//询问哪些行需要编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//设置编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0) {
        return UITableViewCellEditingStyleDelete;
//    }
//    else{
//        return UITableViewCellEditingStyleInsert;
//    }
}
//实现编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    //////////tableview刷新整个cell方法
    //    [self.myTableView reloadData];
    }
    
}


//设置多个按钮
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"点击的是删除");
        
    }];
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"点击的是quxiao");
        //设置为非编辑状态
        self.myTableView.editing = NO;
    }];
    action2.backgroundColor = [UIColor orangeColor];
    NSArray *array = @[action1, action2];
    return array;
}
-(void)createData{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetPaycat&page=1&userid=%@",USERID] success:^(id result) {
//        NSLog(@"%@",result);
        for (NSDictionary *doc in [result[0] objectForKey:@"date"]) {
            ShopCarModel *model = [[ShopCarModel alloc] init];
            [model setValuesForKeysWithDictionary:doc];
            [self.dataArray addObject:model];
        }
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ShopCarModel *model = self.dataArray[indexPath.section];
    
    cell.titleLabel.text = model.GiftName;
    cell.titleLabel.text = model.type;
    cell.preLabel.text = model.GiftMoney;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
